package peer.httpoutput;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.Socket;

public class HttpProcessorLF implements Runnable{
	Thread mainThread;
	OutputStream dos;
	long offset;
	File file;
	Socket socket;
	InputStream dis;
	HttpProcessorLF(Socket socket,File file,OutputStream dos,final InputStream dis,long offset,long length){
		this.offset=offset;
		this.dos=dos;
		this.socket=socket;
		this.dis=dis;
		this.file=file;
	}
	
	public void run(){
		try {
			RandomAccessFile ra;
			ra = new RandomAccessFile(file, "r");
			FileDescriptor fd=ra.getFD();
			ra.seek(offset);
			FileInputStream fis=new FileInputStream(fd);
			byte[] buffer=new byte[1024*1000];
			int length=0;
			while((length=fis.read(buffer))!=-1){
				dos.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			//e.printStackTrace();
		} catch (IOException e) {
			//e.printStackTrace();
		}finally{
			try {
				socket.close();
				dis.close();
				dos.close();
			} catch (IOException e) {
				//e.printStackTrace();
			};
		}
	}
	
}
